package com.ll.blog.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ll.blog.contant.TypeEnum;
import com.ll.blog.entity.Content;
import com.ll.blog.entity.User;
import com.ll.blog.mapper.ContentMapper;
import com.ll.blog.mapper.UserMapper;
import com.ll.blog.model.query.PageQuery;
import com.ll.blog.model.res.ContentListRes;
import com.ll.blog.model.res.PageDto;
import com.ll.blog.service.IContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 * @since 2023-10-25
 */
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements IContentService {
    @Resource
    UserMapper userMapper;
    @Override
    public PageDto<ContentListRes> getContentByPage(Integer id, TypeEnum type, PageQuery query){
        Page<Content> page = Page.of(query.getPage(), query.getPagesize());
        if (StrUtil.isNotBlank(query.getSortby())){
            page.addOrder(new OrderItem(query.getSortby(),query.getIsAsc()));
        }else{
            page.addOrder(new OrderItem("create_time",false));
        }
        LambdaQueryWrapper<Content> wrapper = new LambdaQueryWrapper<>();
        if (id!=null){
            wrapper.eq(Content::getArticleId, id).eq(Content::getParentsId,0).eq(Content::getType,type);
        }else{
            wrapper.eq(Content::getParentsId,0).eq(Content::getType,type);
        }
        Page<Content> p = page(page, wrapper);
        PageDto<ContentListRes> dto = new PageDto<>();
        dto.setTotal(p.getTotal());
        dto.setPages(p.getPages());
        List<Content> records = p.getRecords();
        if (p.getTotal() ==0){
            dto.setList(new ArrayList<>());
        }else{
            ArrayList<ContentListRes> listRes = extracted(records);
            List<ContentListRes> result = getChildren(listRes);
            dto.setList(result);
        }
        return dto;
    }

    private ArrayList<ContentListRes> extracted(List<Content> records) {
        ArrayList<ContentListRes> listRes = new ArrayList<>();
        for (Content record : records) {
            if (record.getUserId()!=null){
                User user = userMapper.selectById(record.getUserId());
                record.setUsername(user.getUsername());
                record.setAvatar(user.getAvatar());
                record.setUserId(null);
            }
            ContentListRes res = new ContentListRes();
            BeanUtils.copyProperties(record,res);
            listRes.add(res);
        }
        return listRes;
    }


    private List<ContentListRes> getChildren(List<ContentListRes> list){
        for (ContentListRes res : list) {
            Integer id = res.getId();
            List<Content> ContenList = lambdaQuery().eq(Content::getParentsId, id).list();
            if (ContenList.isEmpty()){
                res.setChildren(extracted(ContenList));
            }else{
                res.setChildren(getChildren(extracted(ContenList)));
            }
        }
        return list;
    }

}
